Documentation/How It Works.md

How it works

Arduino

The R serial package uses Tcl to allow R to read from and write to the device file. When data is written to the file, the Arduino detects that the buffer is not empty and then parses the line that was sent for the command and the pin/value. The parseArgs function splits the string that was received between commas and returns everything behind the first comma as the command. Everything after the first comma is split and parsed as an integer, which is assigned to the appropriate index of the array that is passed in as the second argument of the function. Because arrays are passed by reference in C/C++, the array that is initialized before the parseArgs command retains the values assigned to it in the function. Depending on which command was sent, the appropriate function is called with the values in the array. Because write functions and the "onServo" command require two arguments, both values of the array will be used in the case of digWrite or anWrite, while only the value at index 0 will be used if digRead or anRead is called. The offServo command requires no arguments, so anything after the command will be ignored. Write commands print to the serial connection, so the data will not automatically show up in R like in the Arduino IDE Serial monitor unless a read command is explicitly performed in R.

R

The Rduino package contains functions to initialize/close a connection, get/set digital and analog pins, operate a servo, generate a square wave, and sample data from the analog pin. The initialization function first detects the specific operating system in order to determine which device file represents the Arduino. It then calls the serialConnection function from the serial package, which calls the Tcl command "fconfigure" to properly set up the serial connection. If the upload parameter is set to TRUE, R loads in the Arduino sketch, replaces the baud rate as specified by the user, and then uploads the modified sketch to the Arduino. The newline parameter is required in order to use String.readStringUntil on the Arduino side, which prevents the delay associated with String.readString. After initialization, the included functions write the corresponding encoded command to the device file. In the case of get commands, after writing to the device file, an infinite loop continues until some value beside NA is read from the devices file. The rduino_close function is simply a wrapper for the close function.

In Between

R - Arduino communication begins in R. The user or the script initiates the serial connection and then either sets a pin to some value or reads in data from a pin. In both scenarios, the encoded command is written to the device file and sent through the usb to the Arduino, which then detects that the serial buffer is not empty and then reads in the command. Depending on the specific command, the Arduino would then physically respond by either setting a pin to the specified voltage, reading in the voltage at the pin, or by initiating/stopping a pulse. In the case of a read command, the Arduino would then also write the value at the pin to the serial connection, which would then be written to the device file. R would have been continually reading in NA from the device file until this point, when the value sent by the Arduino is read, interpreted, and then returned.



pdhoff/Rduino documentation built on May 29, 2019, 7:36 a.m.